package com.kool.kmetrics.metrics.prometheus;

import com.alibaba.fastjson.JSON;
import com.kool.kmetrics.consumer.vo.SessionCntLog;
import com.kool.kmetrics.metrics.MetricsComputer;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Gauge;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author luyu
 */
@Component
public class SessionProm implements MetricsComputer {
    @Resource
    private Gauge sessionGauge;

    @Bean
    private Gauge sessionGauge() {
        return Gauge.build("session_gauge", "session gauge")
                .register(CollectorRegistry.defaultRegistry);
    }

    @Override
    public void compute(String log) {
        SessionCntLog sessionCntLog = JSON.parseObject(log, SessionCntLog.class);
        int cnt = sessionCntLog.getCnt();

        sessionGauge.set(cnt);
    }
}
